<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad"> 
    </avue-crud>
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.driver_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/ems/devicerecord";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: false,
          selection: true,
          menuWidth: 150,
          // menu : false,
          searchIndex:3,
          searchIcon:true,     
          defaultSort:{
            prop: 'updateTime',
            order: 'descending'
          },
          column: [
      
              {
                label: "主键",
                prop: "id",
                hide: true,
                editDisplay: false,
                viewDisplay: true,
                disabled: true,
              },
            
              {
                label: "卡编号",
                prop: "rfid",
                search:true,
                rules: [{
                  required: true,
                  message: "请输入卡编号",
                  trigger: "blur"
                }]
              },

              {
                label: "设备编号",
                prop: "did",
                search:true,
                rules: [{
                  required: true,
                  message: "请输入设备编号",
                  trigger: "blur"
                }]
              },
            
              {
                label: "车辆编号",
                prop: "carNo",
                hide: true,
                rules: [{
                  required: true,
                  message: "请输入车辆编号",
                  trigger: "blur"
                }]
              },
            
              {
                label: "协议类型",
                prop: "lid",
                search:true,
                rules: [{
                  required: false,
                  message: "请输入协议类型",
                  trigger: "blur"
                }]
              },
            
              {
                label: "车牌号",
                prop: "carName",
                search:true,
                rules: [{
                  required: true,
                  message: "请输入车牌号",
                  trigger: "blur"
                }]
              },
            
              // {
              //   label: "站点编号",
              //   prop: "sid",
              //   search:true,/
              //   rules: [{
              //     required: true,
              //     message: "请输入站点编号",
              //     trigger: "blur"
              //   }]
              // },

              {
                label: "趟次数",
                prop: "recCount",
                hide: true,
                addDisplay:false,
                rules: [{
                  required: false,
                  message: "请输入趟次数",
                  trigger: "blur"
                }]
              },
            
              {
                label: "工作状态",
                prop: "workStatus",
                hide: true,
                addDisplay:false, 
                type: "select",
                dicData: [
                  {
                    label: "3在途",
                    value: 3
                  },
                  {
                    label: "2进站等候",
                    value: 2
                  },
                  {
                    label: "1已应班",
                    value: 1
                  },
                  {
                    label: "0未开始",
                    value: 0
                  },
                ],
                rules: [{
                  required: false,
                  message: "请输入工作状态 0未开始 1已应班 2进站等候 3在途 4到达目的地 9故障",
                  trigger: "blur"
                }]
              },
            
              {
                label: "站点状态",
                prop: "stationStatus",
                addDisplay:false, 
                type: "select",
                dicData: [
                  {
                    label: "3已出站",
                    value: 3
                  },
                  {
                    label: "2站台上客",
                    value: 2
                  },
                  {
                    label: "1已进站",
                    value: 1
                  },
                  {
                    label: "0未开始",
                    value: 0
                  },
                ],
                rules: [{
                  required: false,
                  message: "请输入站点状态 0未开始 1已进站 2站台上客 3已出站",
                  trigger: "blur"
                }]
              },
            
              {
                label: "当前时间戳",
                prop: "currTs",
                hide: true,
                addDisplay:false,
                editDisplay:false,
                viewDisplay: false, 
                rules: [{
                  required: false,
                  message: "请输入当前时间戳",
                  trigger: "blur"
                }]
              },
            
              {
                label: "开始时间戳",
                prop: "beginTs",
                hide: true,
                addDisplay:false,
                editDisplay:false,
                viewDisplay: false, 
                rules: [{
                  required: false,
                  message: "请输入开始时间戳",
                  trigger: "blur"
                }]
              },
            
              {
                label: "结束时间戳",
                prop: "endTs",
                hide: true,
                addDisplay:false,
                editDisplay:false,
                viewDisplay: false, 
                rules: [{
                  required: false,
                  message: "请输入结束时间戳",
                  trigger: "blur"
                }]
              },
            
              {
                label: "开始时间",
                prop: "beginDate",
                addDisplay:false, 
                type: "datetime",
                format: "yyyy-MM-dd HH:mm:ss",
                // valueFormat: "yyyy-MM-dd HH:mm:ss",
              },
            
              {
                label: "结束时间",
                prop: "endDate",
                addDisplay:false, 
                type: "datetime",
                format: "yyyy-MM-dd HH:mm:ss",
                // valueFormat: "yyyy-MM-dd HH:mm:ss",
              },
            
            {
              label: "上报时间",
              // prop: "updateTime",
              prop: "currDate",
              hide: true,
              addDisplay:false, 
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              // valueFormat: "yyyy-MM-dd HH:mm:ss",
            },
            
              {
                label: "修改时间",
                prop: "updateTime",
                type: "datetime",
                hide: true,
                addDisplay:false, 
                format: "yyyy-MM-dd HH:mm:ss",
                // valueFormat: "yyyy-MM-dd HH:mm:ss",
              },
            
            //   {
            //     label: "状态",
            //     prop: "status",
            //     hide: true,
            //     addDisplay:false, 
            //     rules: [{
            //       required: false,
            //       message: "请输入状态",
            //       trigger: "blur"
            //     }]
            //   },

            // {
            //   label: "是否删除",
            //   prop: "isDeleted",
            //   type: "radio",
            //   hide: true,
            //   addDisplay:false, 
            //   dicData: [
            //     {
            //       label: "是",
            //       value: 1
            //     },
            //     {
            //       label: "否",
            //       value: 0
            //     },
            //   ],
            // },

            {
              label: "备注",
              prop: "remark",
              labelWidth: 80,
              span: 24,
              minRows: 2,
              type: "textarea",
              hide: true,
              addDisplay:false, 
            },

          ]
        },
        data: []
      };
    },  
  //   created () {
  //   //定时器刷新通知信息 
  //    let me=this
  //   //  me.init()
  //   不要在创建这里执行定时任务，关闭后都还在执行
  //    setInterval(function(){
  //       me.init()
  //    },30*1000);
  // },
    mounted() {
      // 在组件挂载后启动定时器
      this.timer = setInterval(this.init, 45*1000); // 每30秒运行一次任务
    },
    beforeDestroy() {
      // 在组件销毁前清除定时器
      clearInterval(this.timer);
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: true,
          editBtn: true
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {    
      init(){
        console.log('init');
        this.query = {};
        this.onLoad(this.page);
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
